const { app } = require('electron')
const { electronApp, optimizer } = require('@electron-toolkit/utils')

export function initializeApp(mainWindowCreator) {
  const gotTheLock = app.requestSingleInstanceLock()

  if (!gotTheLock) {
    app.quit()
  } else {
    app.on('second-instance', () => {
      // 当运行第二个实例时
      let mainWindow = mainWindowCreator()
      if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore()
        mainWindow.focus()
      }
    })
  }

  app.whenReady().then(() => {
    electronApp.setAppUserModelId('com.electron')
  })

  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  app.on('activate', () => {
    // macOS 特定逻辑
  })

  app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })
}
